<?php

require sfConfig::get('sf_plugins_dir') . DIRECTORY_SEPARATOR . 'trDistributionConfigurePlugin' . DIRECTORY_SEPARATOR. 'lib' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'lessphp' . DIRECTORY_SEPARATOR . 'lessc.inc.php';

class CompileLessTask extends sfBaseTask {

    protected function configure() {
        // // add your own arguments here
        // $this->addArguments(array(
        //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
        // ));

        $this->addOptions(array(
            new sfCommandOption('delete-existing', null, sfCommandOption::PARAMETER_REQUIRED, 'Delete existing css files', false),
        ));

        $this->namespace = 'distribution';
        $this->name = 'compile-less';
        $this->briefDescription = '';
        $this->detailedDescription = <<<EOF
The [CompileLess|INFO] task does things.
Call it with:

  [php symfony CompileLess|INFO]
EOF;
    }

    protected function execute($arguments = array(), $options = array()) {
        $finder = new sfFinder();
        $finder->name('*.less');
        $finder->type('file');
        $this->logSection('searching', sfConfig::get('sf_web_dir'));
        $lessFiles = $finder->in(sfConfig::get('sf_web_dir'));
        if (count($lessFiles) > 0) {
            $this->logSection('process', 'Processing Files');
            foreach ($lessFiles as $lessFilename) {
                $this->logSection('process', $lessFilename);
                $cssFilename = preg_replace('/(.*).less/', '$1.css', $lessFilename);
                //$this->logSection('outputting', $cssFilename);
                if ($options['delete-existing']) {
                    unlink($cssFilename);
                }
                try {
                    lessc::ccompile($lessFilename, $cssFilename);
                } catch (Exception $ex) {
                    $this->logSection('process', $ex->getMessage());
                }
            }
        }
    }

}
